package com.ilarele.shopping.client.provider;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.BaseColumns;

public class ShoppingGenericItemDB implements Parcelable, BaseColumns {

	public static final Uri CONTENT_URI = Uri
			.parse("content://shopping/generic_items");

	public static final String INTERNAL_ID = "internal_id";
	public static final String NAME = "name";
	public static final String DESCRIPTION = "description";
	public static final String DATE = "date_added";
	public static final String MY_RATE = "my_rate";
	public static final String OTHERS_RATE = "others_rate";
	// public static final String BARCODE = "barcode";

	public static final String DEFAULT_SORT_ORDER = DATE + " DESC";

	public String internalId;
	public String name;
	public String description;
	public long dateAdded;
	public int myRate;
	public int othersRate;

	public ShoppingGenericItemDB() {
	}

	private ShoppingGenericItemDB(Parcel in) {
		internalId = in.readString();
		name = in.readString();
		description = in.readString();
		dateAdded = in.readLong();
		myRate = in.readInt();
		othersRate = in.readInt();
	}

	public ContentValues getContentValues() {
		final ContentValues values = new ContentValues();
		values.put(INTERNAL_ID, internalId);
		values.put(NAME, name);
		values.put(DESCRIPTION, description);
		values.put(DATE, dateAdded);
		values.put(MY_RATE, myRate);
		values.put(OTHERS_RATE, othersRate);
		return values;
	}

	public static ShoppingGenericItemDB fromCursor(Cursor c) {
		final ShoppingGenericItemDB item = new ShoppingGenericItemDB();

		item.internalId = c.getString(c.getColumnIndexOrThrow(INTERNAL_ID));
		item.name = c.getString(c.getColumnIndexOrThrow(NAME));
		item.description = c.getString(c.getColumnIndexOrThrow(DESCRIPTION));
		item.dateAdded = c.getLong(c.getColumnIndexOrThrow(DATE));
		item.myRate = c.getInt(c.getColumnIndexOrThrow(MY_RATE));
		item.othersRate = c.getInt(c.getColumnIndexOrThrow(OTHERS_RATE));

		return item;
	}

	public int describeContents() {
		return 0;
	}

	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(internalId);
		dest.writeString(name);
		dest.writeString(description);
		dest.writeLong(dateAdded);
		dest.writeInt(myRate);
		dest.writeInt(othersRate);
	}

	public static final Creator<ShoppingGenericItemDB> CREATOR = new Creator<ShoppingGenericItemDB>() {
		public ShoppingGenericItemDB createFromParcel(Parcel in) {
			return new ShoppingGenericItemDB(in);
		}

		public ShoppingGenericItemDB[] newArray(int size) {
			return new ShoppingGenericItemDB[size];
		}
	};
}
